package com.mybatis.demo;

import com.mybatis.demo.domain.User;
import com.mybatis.demo.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhuYouBin
 * @version 1.0.0
 * @Date: 2022/9/1 16:23
 * @Description
 */
public class BatchDeleteTest {
    public static void main(String[] args) throws IOException {
        // 1、加载配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        // 2、创建 SqlSessionFactoryBuilder 对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        // 3、创建 SqlSessionFactory 对象
        SqlSessionFactory sqlSessionFactory = builder.build(is);
        // 4、创建 SqlSession 对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 5、获取 UserMapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // 执行批量操作
        List<Integer> list = createData();
        int record = userMapper.batchDelete(list);
        System.out.println("record: " + record);
        // 提交事务: mybatis 默认是不会自动提交事务的
        sqlSession.commit();
        // 关闭流
        sqlSession.close();
        is.close();
    }
    private static List<Integer> createData() {
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            list.add(10000 + i);
        }
        return list;
    }
}
